class Game {
    constructor(width, height) {
        this.width = width;
        this.height = height;
        //初始化画布
        this.initcanvas();
        //初始化数据
        this.initData();
        //初始化界面
        this.initView();

    }
    initcanvas() {
        //创建canvas画布
        let canvas = document.createElement("canvas");
        //设置canvas的宽高和位置
        canvas.width = this.width;
        canvas.height = this.height;
        canvas.style.position = "absolute";
        this.disleft = (window.innerWidth - canvas.width) / 2;
        canvas.style.left = this.disleft + 'px';
        canvas.style.top = 0;
        //创建上下文
        this.paint = canvas.getContext('2d');
        this.paint.font = '36px sans-serif';
        this.paint.fillStyle = '#DD9603';
        //获取界面div，讲canvas放在div里
        let div = document.getElementById('game');
        div.appendChild(canvas);
        //canvas点击效果
        canvas.onclick = this.onmousedown.bind(this);
    }
    initData() {
        this.Game_MEUN = 0;
        this.Game_VIEW = this.Game_MEUN + 1;
        this.state = this.Game_MEUN;
    }
    initView() {
        switch (this.state) {
            case this.state = this.Game_MEUN:
                this.view = new GameMeun(this.width, this.height);
                break;
            case this.state = this.Game_VIEW:
                this.view = new GameView(this.width, this.height);
                break;
        }
        this.view.setGameState = this.RState.bind(this);
    }

    RState() {
        if (this.state == this.Game_VIEW) {
            this.state = this.Game_MEUN
        } else {
            this.state = this.Game_VIEW
        }
        this.initView();

    }

    start() {
        setInterval(this.run.bind(this), 200)
    }
    run() {
        this.view.run(this.paint);
    }
    onmousedown(e) {
        let x = e.clientX - this.disleft;
        let y = e.clientY + document.documentElement.scrollTop;
        this.view.onmousedown(x, y);
    }
}